﻿using System;
using System.Collections.Generic;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;

namespace com.huigo.Admin.Web.supplier
{
    public partial class AddSup : BasePage
    {
        private bool isModify
        {
            get
            {
                return sid == long.MinValue ? flgModify.Text == "0" ? false : true : true;
            }
        }

        private long sid
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["sid"]))
                {
                    return long.MinValue;
                }
                else
                {
                    long s = long.MinValue;
                    if (long.TryParse(Request.QueryString["sid"], out s))
                        flgModify.Text = "1";
                    return s;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
                if (isModify)
                {
                    LoadData();
                }
            }
        }

        private void BindList()
        {
            List<DropList> cDropList = Tools.DictToDropList(Dict.DictSupType);
            if (cDropList != null)
            {
                typeList.DataSource = cDropList;
                typeList.DataValueField = "Id";
                typeList.DataTextField = "Value";
                typeList.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!VerifyField(FieldType.STRING, typeList.SelectedValue, true))
            {
                MessageBox.Show(this.Page, "请选择供应商类型！");
                return;
            }
            if (!VerifyField(FieldType.STRING, supName.Text, true))
            {
                MessageBox.Show(this.Page, "供应商名称不能为空！");
                return;
            }
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("supType", typeList.SelectedValue));
            argList.Add(new RequestFuncParams("supName", supName.Text));
            argList.Add(new RequestFuncParams("supMark", supMark.Text));
            argList.Add(new RequestFuncParams("supStatus", rdoStatus.SelectedValue));
            BaseService baseService = new BaseService();
            ParserXml parser;
            if (isModify)
            {
                argList.Add(new RequestFuncParams("sid", sid.ToString()));
                parser = baseService.RequestEntryService(Dict.ERequestFunc.UPDATE_SUP, argList);
            }
            else
            {
                parser = baseService.RequestEntryService(Dict.ERequestFunc.ADD_SUP, argList);
            }
            if (parser != null)
                GoTo(GoToType.ADDSUC);
        }

        private void LoadData()
        {
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("sid", sid.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_SUP_BY_ID, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                typeList.SelectedValue = parser["SupType", 0];
                supName.Text = parser["SupName", 0];
                supMark.Text = parser["SupMark", 0];
                rdoStatus.SelectedValue = parser["SupStatus", 0];
            }

        }
    }
}
